package gupao.concurrency.api.sample;

import gupao.concurrency.utils.Utils;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Proxy;
import java.util.List;
import java.util.concurrent.ExecutionException;

@Slf4j
public class ApiFutureMain {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ApiExecutorService executorService = new ApiExecutorService(4,100);

        ApiMock apiMock = (ApiMock)Proxy.newProxyInstance(ApiMock.class.getClassLoader(), new Class[]{ApiMock.class}, new ApiHandler(new ApiMockImpl()));

        ApiFuture<String> api1 = executorService.subumit(() -> apiMock.api1());
        ApiFuture<String> api2 = executorService.subumit(() -> apiMock.api2());


        String result1 = api1.getResult();
        String result2 = api2.getResult();

        List<ApiInvokeInfo> apiInvokeInfos = ApiExecutorService.ApiFutureTask.getAllMergedApiInfo(api1,api2);
        log.info("results: api1 = {}, api2 = {}", result1, result2);
        apiInvokeInfos.forEach( info -> log.info("{}", info));
    }
}
